package sort;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yi
 * Date: 2024-07-29
 * Time: 10:28
 */
public class QuickSort {

    private static void insertSortRange(int[] array,int start,int end){
        for (int i = start+1; i <= end; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= start; j--) {
                if(tmp < array[j]){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private static void swap(int[] array,int j,int k){
        int tmp = array[j];
        array[j] =  array[k];
        array[k] = tmp;
    }
    public static void sort(int[] array){
        quick(array,0,array.length-1);
    }

    //递归法
    /*private static void quick(int[] array,int start,int end){
        if(start >= end) return;
        if(end-start+1 <= 15) {
            insertSortRange(array,start,end);
            return;
        }
        int mid = midOfThree(array,start,end);
        swap(array,mid,start);
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }*/

    //非递归法 使用栈
    private static void quick(int[] array,int start,int end) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int pivot = partition(array,left,right);
        if(pivot-1 > left){
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot+1 < right){
            stack.push(pivot+1);
            stack.push(right);
        }
        while(!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition(array,left,right);
            if(pivot-1 > left){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot+1 < right){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    private static int midOfThree(int[] array, int start, int end) {
        int mid = (start+end)/2;
        if(array[start] < array[end]) {
            if(array[mid] < array[start]){
                return start;
            }else if(array[mid] > array[end]) {
                return end;
            }else {
                return mid;
            }
        }else {
            if(array[mid] > array[start]) {
                return start;
            }else if(array[mid] < array[end]) {
                return end;
            }else {
                return mid;
            }
        }
    }
    //Hoare法 找标记
    /*private static int  partition(int[] array,int left,int right){
        int key = array[left];
        int i = left;
        while(left < right){
            while(left < right && array[right] >= key){
                right--;
            }
            while(left < right && array[left] <= key){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,i,left);
        return left;
    }*/

    //填坑法  找标记
    private static int  partition(int[] array,int left,int right){
        int key = array[left];
        while(left < right){
            while(left < right && array[right] >= key){
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= key){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = key;
        return left;
    }

    public static void main(String[] args) {

    }
}